<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>添加接口设计</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="../../component/ui/css/ui.css"/>
    </head>
    <body>
        <form class="layui-form" action="" lay-filter="editform">
            <div class="mainBox">
                <div class="main-container">
                    <div class="main-container">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" lay-verify="required|len" autocomplete="off" min="2" placeholder="请输入名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">唯一标志</label>
                            <div class="layui-input-block">
                                <input type="text" name="key" lay-verify="required|len" autocomplete="off" min="2" placeholder="请输入唯一标志" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">所属模块</label>
                            <div class="layui-input-block">
                                <select name="mod_id" lay-search id="mod-area"   lay-verify="required" ></select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">上级权限</label>
                            <div class="layui-input-block">
                                <input type="hidden" name="pid" value="-1" id="pid">
                                <button class="layui-btn" id="chooseRule">
                                    <span id="ruleText">选择权限</span>
                                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                                </button>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">接口方法</label>
                            <div class="layui-input-block">
                                <input type="radio" value="post" name="method" title="POST" checked>
                                <input type="radio" value="get" name="method" title="GET">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">图标</label>
                            <div class="layui-input-block">
                                <input type="text" name="icon" autocomplete="off" class="layui-input icon-selected"  value="layui-icon layui-icon-username"/>
                                <!--input name="icon" id="icon" style="display:none;"        value="layui-icon-face-smile-fine" 
                                class="layui-input" lay-filter="iconPicker"-->
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="api-save">
                        <i class="layui-icon layui-icon-ok"></i>
                    提交
                    </button>
                </div>
            </div>
        </form>
        <script src="../../config/config.js"></script>
        <script src="../../component/layui/layui.js"></script>
        <script src="../../component/ui/ui.js"></script>
        <script>
            layui.use([
                'form', 'layer', 'dropdown', 'iconSelectedPlus'
            ], function () {
                let form = layui.form,
                    layer = layui.layer,
                    $ = layui.$,
                    dropdown = layui.dropdown,
                    iconSelectedPlus = layui.iconSelectedPlus;
                form.verify({
                    len: (val, item) => {
                        let min = item.getAttribute("min")
                        if (val.length < min) {
                            return '长度不能小于' + min;
                        }
                    }
                })
                iconSelectedPlus.render(".icon-selected", {
                    //position : 'right',
                    icons: iconSelectedPlus.icons.layui,
                    simple : true
                });
                 _get(layui, 'api/addBefore', res => {
                    res.modList.forEach(d => {
                            //console.log(d)
                        $("#mod-area").append(new Option(d.name, d.id));
                    });
                    form.render("select");
                    //渲染权限
                    dropdown.render({
                        elem: '#chooseRule',
                        data: res.authTree,
                        trigger: 'hover',
                        click: (d, el) => {
                            console.log(d)
                            $('#ruleText').text(d.title);
                            $('#pid').val(d.id);
                        }
                    })
                });
                //监听提交
                form.on('submit(api-save)', function (data) {
                    data = data.field;
                    _post(layui, 'api/add', data, res => {
                        //console.log(res)
                        parent
                            .layui
                            .table
                            .reload("api-table");
                        parent
                            .layer
                            .close(parent.layer.getFrameIndex(window.name)); //关闭当前页

                    })

                    return false;
                });

            });
        </script>

    </body>
</html>